package com.pj.wisdomlogistics.mvp.model;


import com.google.gson.Gson;
import com.pj.wisdomlogistics.BuildConfig;
import com.pj.wisdomlogistics.entity.response.BaseResponse;
import com.pj.wisdomlogistics.mvp.frame.MvpModel;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;

import java.io.File;
import java.io.IOException;
import java.util.List;

import okhttp3.Response;
import rx.Observable;
import rx.Subscriber;


public class RepairFinishModel extends MvpModel {

    private static final String POST_URL = BuildConfig.SERVER_HOST + "finishRepairApply.do";

    public Observable<BaseResponse> finishRepairApply(
            final String fixWorkerRemark ,
            final int repairApplyID ,
            final List<File> mFiles
    ) {
        return
                Observable.create(new Observable.OnSubscribe<BaseResponse>() {
                    @Override
                    public void call(Subscriber<? super BaseResponse> mSubscriber) {
                        PostFormBuilder formBuilder = OkHttpUtils.post().url(POST_URL);

                        formBuilder.addParams("fixWorkerRemark", fixWorkerRemark + "");

                        if (mFiles != null) {
                            formBuilder.addFiles("mFiles", mFiles);
                        }

                        formBuilder.addParams("repairApplyID", repairApplyID + "");

                        try {
                            Response response = formBuilder.build().execute();
                            String str = response.body().string();
                            BaseResponse<String> postResponse = new Gson().fromJson(str, BaseResponse.class);
                            mSubscriber.onNext(postResponse);
                            mSubscriber.onCompleted();
                        } catch (IOException e) {
                            e.printStackTrace();
                            mSubscriber.onError(e);
                        }
                    }
                });
    }

}
